package de.wbh.gwt.form.layouter.gwt.ui.view;

import java.util.Arrays;

import javax.inject.Inject;

import com.google.gwt.cell.client.AbstractCell;
import com.google.gwt.cell.client.Cell;
import com.google.gwt.core.client.GWT;
import com.google.gwt.resources.client.ClientBundle;
import com.google.gwt.resources.client.CssResource;
import com.google.gwt.resources.client.CssResource.Import;
import com.google.gwt.safehtml.shared.SafeHtmlBuilder;
import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.uibinder.client.UiField;
import com.google.gwt.user.cellview.client.CellList;
import com.google.gwt.user.cellview.client.HasKeyboardSelectionPolicy.KeyboardSelectionPolicy;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.Widget;
import com.google.gwt.view.client.CellPreviewEvent;
import com.google.gwt.view.client.DefaultSelectionEventManager;
import com.google.gwt.view.client.DefaultSelectionEventManager.EventTranslator;
import com.google.gwt.view.client.DefaultSelectionEventManager.SelectAction;
import com.google.gwt.view.client.SingleSelectionModel;

import de.wbh.gwt.form.layouter.gwt.ui.view.LoadStepsViewImpl.Resources.Style;
import de.wbh.gwt.form.layouter.shared.Step;
import de.wbh.gwt.form.layouter.shared.activity.LoadStepsView;

public class LoadStepsViewImpl extends Composite implements LoadStepsView {

	interface LoadStepsViewImplUiBinder extends UiBinder<Widget, LoadStepsViewImpl> {
	}

	public static interface Resources extends ClientBundle {

		public static interface Style extends CssResource {

			String list();

			String panel();

		}

		@Import(CellList.Style.class)
		@Source("loadStepsViewImpl.css")
		Style style();

	}

	private static LoadStepsViewImplUiBinder uiBinder = GWT.create(LoadStepsViewImplUiBinder.class);

	@UiField(provided = true)
	CellList<Step> cellList = new CellList<Step>(new AbstractCell<Step>() {
		@Override
		public void render(Cell.Context context, Step value, SafeHtmlBuilder sb) {
			String text = null;
			switch (value) {
			case UPLOAD_PDF:
				text = "PDF wählen";
				break;
			case UPLOAD_FIELDS:
				text = "Felder bestimmen";
				break;
			case SAVE:
				text = "Konfiguration speichern";
				break;
			default:
				break;
			}

			sb.appendEscaped(text);
		}
	});

	private final SingleSelectionModel<Step> selectionModel = new SingleSelectionModel<Step>();

	@UiField(provided = true)
	Resources resources;

	private final Style style;

	@Inject
	public LoadStepsViewImpl(Resources resources) {
		this.resources = resources;
		this.style = resources.style();
		this.style.ensureInjected();
		initWidget(uiBinder.createAndBindUi(this));
		this.cellList.setRowCount(3);
		this.cellList.setRowData(Arrays.asList(Step.values()));
		this.cellList.setKeyboardSelectionPolicy(KeyboardSelectionPolicy.DISABLED);
		this.cellList.setSelectionModel(this.selectionModel, DefaultSelectionEventManager.createCustomManager(new EventTranslator<Step>() {
			@Override
			public boolean clearCurrentSelection(CellPreviewEvent<Step> event) {
				return false;
			}

			@Override
			public SelectAction translateSelectionEvent(CellPreviewEvent<Step> event) {
				return SelectAction.IGNORE;
			}
		}));
	}

	@Override
	public void setStep(Step step) {
		this.selectionModel.setSelected(step, true);
	}

}
